/**
 * 后端日期时间工具函数
 */

/**
 * 格式化日期
 */
export function formatDate(
  date: Date | string | number,
  format: string = 'YYYY-MM-DD HH:mm:ss'
): string {
  const d = new Date(date);
  
  if (isNaN(d.getTime())) {
    return '';
  }
  
  const year = d.getFullYear();
  const month = String(d.getMonth() + 1).padStart(2, '0');
  const day = String(d.getDate()).padStart(2, '0');
  const hours = String(d.getHours()).padStart(2, '0');
  const minutes = String(d.getMinutes()).padStart(2, '0');
  const seconds = String(d.getSeconds()).padStart(2, '0');
  
  return format
    .replace('YYYY', String(year))
    .replace('MM', month)
    .replace('DD', day)
    .replace('HH', hours)
    .replace('mm', minutes)
    .replace('ss', seconds);
}

/**
 * 获取当前时间戳
 */
export function getCurrentTimestamp(): number {
  return Date.now();
}

/**
 * 获取当前日期时间字符串
 */
export function getCurrentDateTime(format?: string): string {
  return formatDate(new Date(), format);
}

/**
 * 时间戳转日期
 */
export function timestampToDate(timestamp: number): Date {
  return new Date(timestamp);
}

/**
 * 日期转时间戳
 */
export function dateToTimestamp(date: Date | string | number): number {
  return new Date(date).getTime();
}

/**
 * 添加天数
 */
export function addDays(date: Date | string | number, days: number): Date {
  const result = new Date(date);
  result.setDate(result.getDate() + days);
  return result;
}

/**
 * 添加小时
 */
export function addHours(date: Date | string | number, hours: number): Date {
  const result = new Date(date);
  result.setHours(result.getHours() + hours);
  return result;
}

/**
 * 添加分钟
 */
export function addMinutes(date: Date | string | number, minutes: number): Date {
  const result = new Date(date);
  result.setMinutes(result.getMinutes() + minutes);
  return result;
}

/**
 * 添加秒数
 */
export function addSeconds(date: Date | string | number, seconds: number): Date {
  const result = new Date(date);
  result.setSeconds(result.getSeconds() + seconds);
  return result;
}

/**
 * 获取两个日期之间的天数差
 */
export function getDaysDiff(
  startDate: Date | string | number,
  endDate: Date | string | number
): number {
  const start = new Date(startDate);
  const end = new Date(endDate);
  const diffTime = Math.abs(end.getTime() - start.getTime());
  return Math.ceil(diffTime / (1000 * 60 * 60 * 24));
}

/**
 * 获取两个日期之间的小时差
 */
export function getHoursDiff(
  startDate: Date | string | number,
  endDate: Date | string | number
): number {
  const start = new Date(startDate);
  const end = new Date(endDate);
  const diffTime = Math.abs(end.getTime() - start.getTime());
  return Math.ceil(diffTime / (1000 * 60 * 60));
}

/**
 * 获取两个日期之间的分钟差
 */
export function getMinutesDiff(
  startDate: Date | string | number,
  endDate: Date | string | number
): number {
  const start = new Date(startDate);
  const end = new Date(endDate);
  const diffTime = Math.abs(end.getTime() - start.getTime());
  return Math.ceil(diffTime / (1000 * 60));
}

/**
 * 判断是否为今天
 */
export function isToday(date: Date | string | number): boolean {
  const today = new Date();
  const target = new Date(date);
  
  return (
    today.getFullYear() === target.getFullYear() &&
    today.getMonth() === target.getMonth() &&
    today.getDate() === target.getDate()
  );
}

/**
 * 判断日期是否在指定范围内
 */
export function isDateInRange(
  date: Date | string | number,
  startDate: Date | string | number,
  endDate: Date | string | number
): boolean {
  const target = new Date(date);
  const start = new Date(startDate);
  const end = new Date(endDate);
  
  return target >= start && target <= end;
}

/**
 * 获取日期的开始时间（00:00:00）
 */
export function getStartOfDay(date: Date | string | number): Date {
  const result = new Date(date);
  result.setHours(0, 0, 0, 0);
  return result;
}

/**
 * 获取日期的结束时间（23:59:59）
 */
export function getEndOfDay(date: Date | string | number): Date {
  const result = new Date(date);
  result.setHours(23, 59, 59, 999);
  return result;
}

/**
 * 获取月份的第一天
 */
export function getStartOfMonth(date: Date | string | number): Date {
  const result = new Date(date);
  result.setDate(1);
  result.setHours(0, 0, 0, 0);
  return result;
}

/**
 * 获取月份的最后一天
 */
export function getEndOfMonth(date: Date | string | number): Date {
  const result = new Date(date);
  result.setMonth(result.getMonth() + 1, 0);
  result.setHours(23, 59, 59, 999);
  return result;
}

/**
 * 获取年份的第一天
 */
export function getStartOfYear(date: Date | string | number): Date {
  const result = new Date(date);
  result.setMonth(0, 1);
  result.setHours(0, 0, 0, 0);
  return result;
}

/**
 * 获取年份的最后一天
 */
export function getEndOfYear(date: Date | string | number): Date {
  const result = new Date(date);
  result.setMonth(11, 31);
  result.setHours(23, 59, 59, 999);
  return result;
}

/**
 * 解析日期字符串
 */
export function parseDate(dateString: string): Date | null {
  // 支持多种日期格式
  const formats = [
    /^\d{4}-\d{2}-\d{2}$/, // YYYY-MM-DD
    /^\d{4}\/\d{2}\/\d{2}$/, // YYYY/MM/DD
    /^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$/, // YYYY-MM-DD HH:mm:ss
    /^\d{4}\/\d{2}\/\d{2} \d{2}:\d{2}:\d{2}$/, // YYYY/MM/DD HH:mm:ss
  ];
  
  const isValidFormat = formats.some(format => format.test(dateString));
  if (!isValidFormat) {
    return null;
  }
  
  const date = new Date(dateString);
  return isNaN(date.getTime()) ? null : date;
}

/**
 * 验证日期字符串格式
 */
export function isValidDateString(dateString: string): boolean {
  return parseDate(dateString) !== null;
}

/**
 * 获取UTC时间戳
 */
export function getUTCTimestamp(date?: Date | string | number): number {
  const d = date ? new Date(date) : new Date();
  return d.getTime();
}

/**
 * 转换为UTC日期
 */
export function toUTCDate(date: Date | string | number): Date {
  const d = new Date(date);
  return new Date(d.getTime() + d.getTimezoneOffset() * 60000);
}

/**
 * 从UTC转换为本地时间
 */
export function fromUTCDate(date: Date | string | number): Date {
  const d = new Date(date);
  return new Date(d.getTime() - d.getTimezoneOffset() * 60000);
}
